<template>
    <div class="box">
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple">
                    <div class="kuang"></div>
                    <div class="box_text">
                        {{ title }}
                        <el-divider></el-divider>
                    </div>
                    <div style="margin-top: 20px; margin-left: 40px; width: 500px">
                        <el-form label-position="right" label-width="100px">
                            <el-form-item label="违章车辆">
                                <el-select v-model="form.illegalNumber">
                                    <el-option v-for="item in carNumbers" :key="item.id" :label="item.carNumber"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="违章日期">
                                <el-date-picker v-model="form.illegalTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                                    placeholder="违章日期">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="违章地点">
                                <el-input v-model="form.illegalUnit" placeholder="违章地点"
                                    style="width: 220px;"></el-input>
                            </el-form-item>
                            <el-form-item label="违章行为">
                                <el-input type="textarea" v-model="form.illegalBehavior" maxlength="100" show-word-limit
                                    placeholder="违章地点"></el-input>
                            </el-form-item>
                            <el-form-item label="扣分">
                                <el-input v-model="form.illegalDeduct" placeholder="违章地点"
                                    style="width: 220px;"></el-input> 分
                            </el-form-item>
                            <el-form-item label="罚款">
                                <el-input v-model="form.illegalFine" placeholder="违章地点"
                                    style="width: 220px;"></el-input> 元
                            </el-form-item>
                            <el-form-item label="状态">
                                <el-radio v-model="form.illegalState" label="0">未处理</el-radio>
                                <el-radio v-model="form.illegalState" label="1">已处理</el-radio>
                            </el-form-item>
                                <el-form-item label="违章人">
                                <!-- 选择负责人start -->
                                <div class="avatar-uploader">
                                    <!-- 选中负责人后展示start -->
                                    <div v-if="selectedUser != ''">
                                        <img :src="selectedUser.icon" class="avatar" @click="headPage = true">
                                        <span style="position: relative;top: -17px;left: 20px;">{{ selectedUser.nickName
                                            }}
                                            <span style="margin-left: 26px;cursor: pointer;" @click="deUser">X</span>
                                        </span>
                                    </div>
                                    <!-- 选中负责人后展示end -->
                                    <!-- 负责人未选中展示 -->
                                    <i v-else @click="headPage = true" class="el-icon-plus avatar-uploader-icon"></i>
                                </div>
                            </el-form-item>

                            <el-dialog v-loading="loading" element-loading-text="拼命加载中"
                                element-loading-spinner="el-icon-loading"
                                element-loading-background="rgba(0, 0, 0, 0.8)" :visible.sync="headPage" width="50%"
                                :show-close="false">
                                <el-breadcrumb separator-class="el-icon-arrow-right">
                                    <!-- 步骤条start -->
                                    <el-breadcrumb-item v-for="(value, index) in headNavs" :key="index + 'e'"
                                        @click="upNav1(index)">
                                        <el-button v-if="persons.id == undefined" size="mini" style="border: 0px;"
                                            @click="upNav1(index)">{{ value }}</el-button>
                                    </el-breadcrumb-item>
                                    <!-- 步骤条end -->
                                    <br>
                                    <br>
                                    <br>
                                    <!-- 展示右边所选中的用户start -->
                                    <div style="float: right;margin-top: -70px;margin-right:30%;color: black;">
                                        已选择<span v-if="selectedUser != ''">1</span><span v-else>0</span>位联系人
                                        <br>
                                        <br>
                                        <br>
                                    </div>
                                    <!-- 展示选中的用户 -->
                                    <div v-if="selectedUser != ''" class="architecture"
                                        style="height: 50px;float: right;    position: relative;top: 12px;right: 50px;">
                                        <img style="width: 30px;height: 30px;position: relative;top: 10px;"
                                            :src="selectedUser.icon" />
                                        <span
                                            style="margin-left: 17%;color: black;color: black;font-size: 10px;display: block;margin-top: -20px;">
                                            {{ selectedUser.nickName }}<br><br>
                                            {{ selectedUser.tposition.deptName }}
                                            <span
                                                style="float: right;margin-top: -10px;margin-right: 20px;cursor: pointer;"
                                                @click="deUser">X</span>
                                        </span>

                                    </div>
                                    <!-- 展示右边所选中的用户end -->
                                    <!-- 展示左边信息start -->
                                    <!-- 展示部门和人数start -->
                                    <div class="architecture" style="line-height: 39px;"
                                        v-for="(value, perIndex) in persons" :key="perIndex"
                                        @click="selectArchitecture(value.deptName)">
                                        <div class="log">
                                            <img
                                                src="https://cdn7.axureshop.com/demo2023/2236859/images/%E8%AF%B7%E5%81%87/u2303.svg" />
                                        </div>
                                        <span style="margin-left: 5%;color: black;">
                                            {{ value.deptName }}
                                        </span>
                                        &nbsp;&nbsp;&nbsp;({{ value.deptCount }})
                                        <span style="float: right;margin-right: 20px;">
                                            ＞
                                        </span>
                                    </div>
                                    <!-- 展示部门和人数end -->
                                    <br>
                                    <!-- 展示当前所选部门的所有用户start -->
                                    <div v-if="users.length != 0">
                                        <div :class="[selectArchitectureCss != userIndex ? 'architecture' : '', selectArchitectureCss == userIndex ? 'architectureActe' : '']"
                                            style="height: 50px;" v-for="(myUser, userIndex) in users" :key="userIndex"
                                            @click="selectUser(userIndex)">
                                            <el-radio style="width: 80%;height: 100%;" v-model="selectId"
                                                :label="userIndex">
                                                &nbsp;&nbsp;&nbsp;
                                                <img style="width: 30px;height: 30px;position: relative;top: 10px;"
                                                    :src="myUser.icon" />
                                                <span
                                                    style="margin-left: 5%;color: black;font-size: 10px;float: left;position: relative;top: 12px;left: 110px;">
                                                    {{ myUser.nickName }}<br><br>
                                                    {{ myUser.tposition.deptName }}
                                                </span>

                                            </el-radio>
                                            <span>
                                                ＞
                                            </span>
                                        </div>
                                    </div>
                                    <!-- 展示当前所选部门的所有用户start -->
                                    <!-- 展示左边信息end -->

                                </el-breadcrumb>

                                <!-- 竖直分割线 -->
                                <div class="myHr"></div>


                                <span slot="footer" class="dialog-footer">
                                    <el-button @click="headPage = false">取 消</el-button>
                                    <el-button type="primary" @click="headPage = false">确 定</el-button>
                                </span>
                            </el-dialog>
                            <!-- 选择负责人end -->
                        </el-form>
                    </div>
                    <div class="kuang"></div>
                    <div class="box_text">
                        更多信息
                        <el-divider></el-divider>
                    </div>
                    <div style="margin-top: 20px; margin-left: 40px; width: 500px">
                        <el-form label-position="right" label-width="100px">
                            <el-form-item label="备注">
                                <el-input type="textarea" v-model="form.illegelRambak"
                                    :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入内容">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="违章凭证">
                                <el-upload class="avatar-uploader" action="http://localhost:8030/ftp/upload"
                                    :show-file-list="false" :on-success="handleAvatarSuccess"
                                    :before-upload="beforeAvatarUpload" :headers="upheaders">
                                    <img v-if="imageUrl" :src="form.illegalImg" class="avatar" />
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    <div slot="tip" class="el-upload__tip">
                                        只能上传jpg/png文件，且不超过500kb
                                    </div>
                                </el-upload>
                            </el-form-item>
                        </el-form>
                        <el-button type="primary" @click="onSubmit">立即提交</el-button>
                        <el-button @click="back">取消</el-button>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import axios from "axios";
export default {
    data() {
        return {
            form: {
                createUser: JSON.parse(localStorage.getItem("user")).id
            },
            options: {},
            title: "添加违章信息",
            carNumbers: {},
            imageUrl: false,
            upheaders: {
                Authorization: "Bearer" + localStorage.getItem("token"),
            },
            radio:"",
            // 获取负责人start
            headPage: false,//开启选择负责人页面
            headNavs: ["组织架构"],//步骤条
            persons: [],//部门和部门人数
            users: [],//所选部门的人员
            loading: false,//加载开启和关闭
            selectArchitectureCss: -1,//选中指定行后开启指定的css
            selectId: -1,//单选框
            selectedUser: "",//选中的用户
            replacementCard: {
                rulesName: "",
                replacementHead: "",
                replacementType: [],
                card: false,
            },
            //  获取负责人end
        };
    },
    created() {
        this.getCarNumber()
        this.getDept()
        if (this.$route.query.id != undefined) {
            this.title = "修改违章信息"
            this.echoCarcheer()
        }
    },
    methods: {
        //回显
        echoCarcheer() {
            axios.post("carillegal/echoillegal?id=" + this.$route.query.id).then((res) => {
                axios.post("/demo/findOneUser?id="+res.data.data.illegalUser)
                .then((res)=>{
                        this.selectedUser = res.data;
                })
                this.form = res.data.data
                this.imageUrl = true
            })
        },
        //提交
        onSubmit() {
            if (this.$route.query.id == undefined) {
                axios.post("carillegal/addillegal", this.form).then((res) => {
                    console.log(res)
                    if (res.data.code === 200) {
                        this.$message.success(res.data.data)
                        this.form = {}
                        this.$router.go(-1)
                    }
                })
            } else {
                this.form.id = this.$route.query.id
                axios.post("carillegal/upillegal", this.form).then((res) => {
                    console.log(res)
                    if (res.data.code === 200) {
                        this.$message.success(res.data.data)
                        this.form = {}
                        this.$router.go(-1)
                    }
                })
            }
        },
        //查询车牌号
        getCarNumber() {
            axios.get("/car/getCarNumber").then((res) => {
                console.log(res)
                this.carNumbers = res.data.data
            })
        },
        // 返回上一页
        back() {
            this.$router.go(-1)
        },
        handleAvatarSuccess(res, file) {
            console.log(res);
            if (res != "上传失败") {
                this.$message.success("上传成功");
                this.form.illegalImg = res;
                this.imageUrl = true;
            } else {
                this.imageUrl = false;
                this.$message.error(res);
            }
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === "image/jpeg" || "image/png";
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG或者png 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 500kb!");
            }
            return isJPG && isLt2M;
        },
        // 负责人start
        // 更新步骤条
        upNav(index) {
            this.headNavs = this.headNavs.slice(0, index + 1);
            if ("组织架构" == this.headNavs) {
                this.loading = true;
                this.users = [];
                this.getDept();
            }
        },
        // 获取部门及部门人数
        getDept() {
            this.selectId = -1;
            this.deUser();
            axios.post("/ReplacementCard/findDept")
                .then((res) => {
                    this.persons = res.data.rows;
                    this.loading = false;
                })
        },
        // 获取所选择部门的用户信息
        selectArchitecture(value) {
            this.loading = true;
            this.persons = [];
            this.headNavs.push(value);
            var data = new FormData();
            data.append("name", value)
            axios.post("/ReplacementCard/findUserAndDept", data)
                .then((res) => {
                    this.users = res.data.rows;
                    if (this.users != []) {
                        this.loading = false;
                    }
                })

        },
        // 是否允许补卡更改重置和他相关联的值
        reset() {
            if (!this.replacementCard.card) {
                this.replacementCard.replacementCount = undefined;
                this.replacementCard.time = undefined;
            }

        },

        // 选中用户
        selectUser(index) {
            this.selectArchitectureCss = index;
            this.selectedUser = this.users[index];
            this.form.illegalUser = this.selectedUser.id;
        },
        // 删除用户
        deUser() {
            this.selectId = -1;
            this.selectedUser = "";
            this.selectArchitectureCss = -1;
            this.form.illegalUser = "";
        },
        // 负责人end
    }
};
</script>

<style scoped>
/* element-ui样式 */
.el-row {
    margin-bottom: 10px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple {
    background: #fff;
    border-radius: 10px;
}

/* 自定义样式 */
/* * {
      margin: 0px;
      padding: 0px;
    } */
.box {
    width: 98%;
    height: 100%;
    margin: auto;
    /* background-color: red; */
}

.box_text {
    padding-top: 20px;
    /* padding-left: 40px; */
    margin-left: 40px;
    font-size: 13px;
}

/* 获取负责人start */
.avatar-uploader-icon[data-v-0bfb98ce] {
    border: 1px solid rgba(198, 201, 208, 1);
    background-color: rgba(248, 248, 248, 1);
    border-radius: 50%;
}

.avatar-uploader-icon {
    font-size: 20px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
}


.architecture {
    width: 271px;
    height: 39px;
    padding-left: 20px;
    border-radius: 5px;
}

.architecture:hover {
    background-color: #f8f8f8;
}

.architectureActe {
    width: 271px;
    height: 39px;
    line-height: 39px;
    padding-left: 20px;
    border-radius: 5px;
    background-color: rgba(231, 244, 255, 1);
    ;
}

/* 竖着的水平线 */
.myHr {
    border-left: 2px solid#989DAA;
    ;
    height: 100%;
    left: 48%;
    top: 0px;
    position: absolute;
}

.log {
    width: 27px;
    height: 27px;
    background-color: rgba(158, 208, 255, 1);
    float: left;
    position: relative;
    top: 15%;
    border-radius: 50%;
}

.log img {
    width: 13px;
    height: 13px;
    position: relative;
    bottom: 15%;
    left: 25%;
}

/* 获取负责人end */
.kuang {
    width: 4.5px;
    height: 25px;
    background-color: #409EFF;
    margin-left: 30px;
    position: relative;
    top: 43px;
}
</style>